/**
 * Copyright (c) 2013-Now http://aidex.vip All rights reserved.
 */
import config from '@/common/config.js';
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

let lifeData = {};
let phoneLayout = {}
const systemInfo = uni.getSystemInfoSync();

try {
	lifeData = uni.getStorageSync('lifeData');
	// 尝试获取本地是否存在lifeData变量，第一次启动APP时是不存在的
	phoneLayout = {
		isIpx: systemInfo.model.indexOf('iphone X') > -1 ? true : false,
		navHeight: systemInfo.statusBarHeight,
		ipxbottom: Math.abs(systemInfo.safeAreaInsets.bottom),
		safeAreaHeight: systemInfo.safeArea.height,
		screenPaddingTop: systemInfo.statusBarHeight + 44
	}
} catch (e) {
	phoneLayout = {
		isIpx: false,
		navHeight: 20,
		ipxbottom: 34,
		safeAreaHeight: 750,
		screenPaddingTop: 64
	}
}
console.log(phoneLayout)
// 添加订单参数
let addOrder = {
	"addrId": 0,
	"code": "",
	"couponId": 0,
	"goodTypeList": [],
	"isAdditional": 0, // 是否加急
	"kg": 0,
	"openId": "",
	"payType": 1,
	"remark": "",
	"requestedFlg": 1,
	"requestedTime": "",
	"serviceType": 0, // serviceType 0送1收
	"siteId": 0,
	"tip": 0,
	"deliveryFlg": 0,
	"logisticsId": ''
}
// 需要永久存储，且下次APP启动需要取出的，在state中的变量名
let saveStateKeys = ['vuex_user', 'vuex_info', 'vuex_token', 'vuex_tokenHead', 'vuex_openId',
	'vuex_remember', 'vuex_locale', 'vuex_isAgent',
	'vuex_identity', 'vuex_addOrder', 'vuex_site', 'vuex_logistics',
	'vuex_sendOrGet', 'vuex_myAddress', "vuex_client", 'vuex_safeArea',
	'vuex_goodTypeList', 'vuex_flag', 'vuex_remark', 'vuex_kg', 'vuex_code',
	'vuex_h5Config'
];

// 保存变量到本地存储中
const saveLifeData = function (key, value) {
	// 判断变量名是否在需要存储的数组中
	if (saveStateKeys.indexOf(key) != -1) {
		// 获取本地存储的lifeData对象，将变量添加到对象中
		let tmp = uni.getStorageSync('lifeData');
		// 第一次打开APP，不存在lifeData变量，故放一个{}空对象
		tmp = tmp ? tmp : {};
		tmp[key] = value;
		// 执行这一步后，所有需要存储的变量，都挂载在本地的lifeData对象中
		uni.setStorageSync('lifeData', tmp);
	}
}
// 简化 vuex 操作，文档：https://uviewui.com/components/vuexDetail.html
const store = new Vuex.Store({
	state: {
		// 如果上面从本地获取的lifeData对象下有对应的属性，就赋值给state中对应的变量
		// 用户信息
		vuex_user: lifeData.vuex_user ? lifeData.vuex_user : {
			userName: ''
		},
		vuex_info: lifeData.vuex_info ? lifeData.vuex_info : {},
		vuex_token: lifeData.vuex_token ? lifeData.vuex_token : '',
		vuex_openId: lifeData.vuex_openId ? lifeData.vuex_openId : '',
		vuex_tokenHead: lifeData.vuex_tokenHead ? lifeData.vuex_tokenHead : '',
		vuex_remember: lifeData.vuex_remember ? lifeData.vuex_remember : '',
		vuex_locale: lifeData.vuex_locale ? lifeData.vuex_locale : '',
		vuex_isAgent: lifeData.vuex_isAgent ? lifeData.vuex_isAgent : '',
		vuex_phoneLayout: phoneLayout,
		vuex_config: config,
		// 身份 暂定 1跑腿员 2用户
		vuex_identity: lifeData.vuex_identity ? lifeData.vuex_identity : '',
		// 添加order的入参基础数据
		vuex_addOrder: lifeData.vuex_addOrder ? lifeData.vuex_addOrder : JSON.parse(JSON.stringify(addOrder)),
		// 选择的站点信息
		vuex_site: lifeData.vuex_site ? lifeData.vuex_site : {},
		// 送或取 send   get
		vuex_sendOrGet: lifeData.vuex_sendOrGet ? lifeData.vuex_sendOrGet : '',
		// 我的默认地址
		vuex_myAddress: lifeData.vuex_myAddress ? lifeData.vuex_myAddress : {},
		// 商品类型
		vuex_goodTypeList: lifeData.vuex_goodTypeList ? lifeData.vuex_goodTypeList : [],
		// 商品备注
		vuex_remark: lifeData.vuex_remark ? lifeData.vuex_remark : '',
		// 商品重量
		vuex_kg: lifeData.vuex_kg ? lifeData.vuex_kg : 0,
		// 取件码
		vuex_code: lifeData.vuex_code ? lifeData.vuex_code : '',
		// 快递公司下拉 首页init接口获取
		vuex_logistics: lifeData.vuex_logistics ? lifeData.vuex_logistics : [],
		vuex_flag: lifeData.vuex_flag ? lifeData.vuex_flag : 'user',
		vuex_client: lifeData.vuex_client ? lifeData.vuex_client : {},
		vuex_h5Config: lifeData.vuex_h5Config ? lifeData.vuex_h5Config : {},
		vuex_safeArea: lifeData.vuex_safeArea ? lifeData.vuex_safeArea : {
			top: 0,
			bottom: 0
		},
		// 自定义tabbar数据
		// vuex_tabbar: [{
		// 		iconPath: "/static/uview/example/component.png",
		// 		selectedIconPath: "/static/uview/example/component_select.png",
		// 		text: '组件',
		// 		pagePath: '/pages/example/components'
		// 	},
		// 	{
		// 		iconPath: "/static/uview/example/js.png",
		// 		selectedIconPath: "/static/uview/example/js_select.png",
		// 		text: '工具',
		// 		midButton: true,
		// 		pagePath: '/pages/example/js'
		// 	},
		// 	{
		// 		iconPath: "/static/uview/example/template.png",
		// 		selectedIconPath: "/static/uview/example/template_select.png",
		// 		text: '模板',
		// 		pagePath: '/pages/example/template'
		// 	}
		// ]
	},
	mutations: {
		$uStore(state, payload) {
			// 判断是否多层级调用，state中为对象存在的情况，诸如user.info.score = 1
			let nameArr = payload.name.split('.');
			let saveKey = '';
			let len = nameArr.length;
			if (len >= 2) {
				let obj = state[nameArr[0]];
				for (let i = 1; i < len - 1; i++) {
					obj = obj[nameArr[i]];
				}
				obj[nameArr[len - 1]] = payload.value;
				saveKey = nameArr[0];
			} else {
				// 单层级变量，在state就是一个普通变量的情况
				state[payload.name] = payload.value;
				saveKey = payload.name;
			}
			// 保存变量到本地，见顶部函数定义
			saveLifeData(saveKey, state[saveKey])
		},
		INIT: state => {
			state.vuex_user = {}
			state.vuex_info = {}
			state.vuex_token = ''
			state.vuex_openId = ''
			state.vuex_tokenHead = ''
			state.vuex_addOrder = JSON.parse(JSON.stringify(addOrder))
			state.vuex_site = {}
			state.vuex_sendOrGet = ''
			state.vuex_myAddress = {}
			state.vuex_goodTypeList = []
			state.vuex_remark = ''
			state.vuex_kg = ''
			state.vuex_code = ''
		}
	},
	actions: {
		init({
			commit
		}) {
			commit('INIT')
		}
	}
})

export default store
